package com.glsc.ngateway.oaflow.utils;

import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;

/**
 * @version V1.0
 * @desc AES 加密工具类
 */

public class EncryptUtils {

    public static final String KEY = "qJzGEh6hESZDVJeCnFPGuxza";
    public static final String encoding = "utf-8";
    private static final String ALGORITHM = "DESede";
    private static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";

    public static String encryptString(String orgStr) {
        return Base64.encodeBase64String(encrypt(orgStr.getBytes()));
    }

    public static String decryptString(String str) {
        return new String(decrypt(Base64.decodeBase64(str)));
    }

    public static byte[] encrypt(byte[] originalText) {
        try {
            SecretKey deskey = new SecretKeySpec(build3DesKey(KEY), SymmetricAlgorithm.DESede.getValue());
            Cipher cipher = Cipher.getInstance(SymmetricAlgorithm.DESede.getValue());
            cipher.init(Cipher.ENCRYPT_MODE, deskey);
            return cipher.doFinal(originalText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static byte[] decrypt(byte[] cipherText) {
        try {
            SecretKey deskey = new SecretKeySpec(build3DesKey(KEY), SymmetricAlgorithm.DESede.getValue());
            Cipher cipher = Cipher.getInstance(SymmetricAlgorithm.DESede.getValue());
            cipher.init(Cipher.DECRYPT_MODE, deskey);
            return cipher.doFinal(cipherText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException {
        byte[] key = new byte[24];
        byte[] temp = keyStr.getBytes("utf-8");
        if (key.length > temp.length) {
            System.arraycopy(temp, 0, key, 0, temp.length);
        } else {
            System.arraycopy(temp, 0, key, 0, key.length);
        }
        return key;
    }

    public static String byte2Hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
            if (n < b.length - 1) ;
        }
        return hs.toUpperCase();
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String msg = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><MobileLogon xmlns=\"http://www.xquant.com/\"><aUserCode>Apptest</aUserCode><aPwd><![CDATA[1]]></aPwd><aMAC>|82:54:01:16:59:54|895d6288e1034cb6|</aMAC></MobileLogon></soap:Body></soap:Envelope>";
        System.out.println("前" + msg);

        byte[] secretArr = EncryptUtils.encrypt(msg.getBytes());
        // String str = byte2Hex(secretArr);
        System.out.println("后" + Base64.encodeBase64String(secretArr));

        String dd = Base64.encodeBase64String(secretArr);
        dd = "2Nw+2cKNqocqUx2SSXJKTf+3WRdXOeYKVvgwmCrJplGDoI/wluFDNf/5Pp2f//8/+c5RW074Iv5/zPu/Zj88lJPw9L4V/KUBNR15wGR+FWyrikJtV4+WuJhDkI9XLNGCwycDuRfsidjJd6ZtFrWZl+0FRPX3DqqwVUmZBomnLdOW90WFxB2Hoc85yoKBcLEUfLcqNOITbQRuDQDCNlKk359M/2aNq42Bv/DcVq9DLdsTBBYZh2KSP/QTL8AmclagWDv8smA8z4/LeZeagwkibGJO/6NkLP+pnFKyMXN9l6pRNUTTU9Pm1L8FWQzfdRCbVUmZBomnLdMb38h9bOadBp3biBPx89DZJRmQpTlXZLOqErJrDKGMVRCTTItsROewTzY0Y9GWi+TH85qJqp6FeWFJU91e87DCX7eRvCskyC2RBoXFYFOq1adSX+6PuyKzYlnHKg3yHEBjheIG4KmzTVUUvzGBN44tooLiV3ul+1XsRqjNiB/qvIKZcyo/5vyOk+n4XmIZEjoctqKVP0c2masbEcMTpLBy5e4lRnjjMqs=";
        byte[] myMsgArr = EncryptUtils.decrypt(Base64.decodeBase64(dd));
        System.out.println("后" + new String(myMsgArr));


        System.out.println(encryptString(msg));
        System.out.println(decryptString(encryptString(msg)));


        System.out.println(Base64.encodeBase64String("1".getBytes("utf-8")));
        System.out.println(new String(Base64.decodeBase64(Base64.encodeBase64String("1".getBytes()))));

    }
}
